package com.suyang.syt.aop;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.suyang.syt.model.OperateLog;
import com.suyang.syt.util.PropertiesUtil;

public class OperateLogAspectUtil {

	private static Logger logger = Logger.getLogger(OperateLogAspectUtil.class);

	private OperateLogAspectUtil() {

	}

	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {
		try {
			driver = PropertiesUtil.get("jdbc.properties", "jdbc.driverClassName");
			url = PropertiesUtil.get("jdbc.properties", "jdbc.url");
			user = PropertiesUtil.get("jdbc.properties", "jdbc.username");
			password = PropertiesUtil.get("jdbc.properties", "jdbc.password");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		Connection connection = null;
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (preparedStatement != null) {
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean addLogException(OperateLog operateLog) {
		String sql = " insert into SYS_OPERATE_LOG(createName,createTime,ip,action,exception,status,operate) values(?,?,?,?,?,?,?) ";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, operateLog.getCreateName());
			preparedStatement.setString(2, operateLog.getCreateTime());
			preparedStatement.setString(3, operateLog.getIp());
			preparedStatement.setString(4, operateLog.getAction());
			preparedStatement.setString(5, operateLog.getException());
			preparedStatement.setInt(6, operateLog.getStatus());
			preparedStatement.setString(7, operateLog.getOperate());
			int result = preparedStatement.executeUpdate();
			if (result == 1) {
				return true;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			closeAll(connection, preparedStatement, null);
		}
		return false;
	}

}